//
//  HYLPageScroll.m
//  UIScrollView测试
//
//  Created by hyl on 16/5/2.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "HYLPageScroll.h"

@interface HYLPageScroll ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;

@property (weak, nonatomic) IBOutlet UIScrollView *pageScroll;

//@property (nonatomic,strong) NSArray *picturesName;

@end

@implementation HYLPageScroll
+(instancetype)pageScroll{
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil]firstObject];
}
-(void)setPictures:(NSArray *)pictures{
    _pictures=pictures;
    for (int i=0; i<pictures.count; i++) {
        UIImageView *imageView=[[UIImageView alloc]init];
        imageView.image=[UIImage imageNamed:pictures[i]];
        [self.pageScroll addSubview:imageView];
    }
//    self.pageScroll.contentSize=CGSizeMake(pictures.count*300, 0);
    self.pageScroll.pagingEnabled=YES;
//    self.pageScroll.directionalLockEnabled=NO;
    self.pageScroll.showsHorizontalScrollIndicator=NO;
    self.pageControl.numberOfPages=pictures.count;
    self.pageScroll.delegate=self;//设置代理
}
-(void)layoutSubviews{
    [super layoutSubviews];
    self.pageScroll.frame=self.bounds;
    CGFloat scrollWidth=self.pageScroll.frame.size.width;
    CGFloat scrollHeight=self.pageScroll.frame.size.height;
    CGFloat controlWidth=100;
    CGFloat controlHeight=20;
    for (int i=0; i<self.pageScroll.subviews.count; i++) {
        self.pageScroll.subviews[i].frame=CGRectMake(i*scrollWidth, 0, scrollWidth, scrollHeight);
    }
    self.pageScroll.contentSize=CGSizeMake(self.pictures.count*scrollWidth, 0);
    self.pageControl.frame=CGRectMake(scrollWidth-controlWidth, scrollHeight-controlHeight, controlWidth,controlHeight);
}
/** 监视 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
//    NSLog(@"%f",scrollView.contentOffset.x);
    self.pageControl.currentPage=((scrollView.contentOffset.x/self.pageScroll.frame.size.width)+0.5);
}
@end
